<template>
	<view class="">
		<!-- 搜索热区组件的使用 -->
		<SearchLink />
		<!-- 主体部分，左右两侧分别滚动 -->
		<!-- 请求回来后再渲染 -->
		<view class="content" v-if="categories.length">
			<view class="left">
				<view class="cate1" :class="{active:activeIndex===index1}" v-for="(cate1,index1) in categories" :key="index1"
				 @click="changeIndex(index1)">
					{{cate1.cat_name}}
				</view>
			</view>
			<view class="right">
				<image src="/static/images/titleImage.png" mode=""></image>
				<!-- 二级分类 -->
				<view class="cate2" v-for="(cate2,index2) in categories[activeIndex].children" :key="index2">
					<view class="cate2-name">
						/<text>{{cate2.cat_name}}</text>/
					</view>
					<!-- 三级分类 -->
					<view class="cate3-wrapper">
						<view class="cate3" v-for="(cate3,index3) in cate2.children" :key="index3" @click="toSearchList(cate3.cat_name)">
							<image :src="cate3.cat_icon"></image>
							<text>{{cate3.cat_name}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 引入SearchLink组件
	import SearchLink from '@/components/SearchLink.vue'
	export default {
		components: {
			SearchLink
		},
		data() {
			return {
				// 当前一级分类选中的下标
				activeIndex: 0,
				// 分类的数据
				categories: [],
			}
		},
		onLoad() {
			this.getCategories()
		},
		methods: {
			// 跳转到搜索列表页
			toSearchList(catName) {
				uni.navigateTo({
					url: '/pages/search_list/search_list?keyword=' + catName
				})
			},
			async getCategories() {
				this.categories = await this.$request({
					url: '/api/public/v1/categories'
				})
			},
			changeIndex(index) {
				this.activeIndex = index
			}
		}

	}
</script>

<style lang="less">
	.content {
		display: flex;
		// 在当前情况下,就是相对于屏幕的
		position: absolute;
		left: 0;
		right: 0;
		top: 100rpx;
		bottom: 0;

		.left {
			width: 198rpx;
			overflow: auto;

			.cate1 {
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				box-sizing: border-box;
				background-color: #f4f4f4;
				color: #333;
				border-bottom: 1rpx solid #eee;

				//大概相当于 .cate1.active
				&.active {
					color: #eb4450;
					background-color: #fff;
					position: relative;

					&::before {
						position: absolute;
						content: '';
						width: 8rpx;
						height: 60rpx;
						background-color: #eb4450;
						left: 0;
						top: 20rpx;
					}
				}
			}
		}

		.right {
			// 占满剩下的宽度
			flex: 1;
			overflow: scroll;

			>image {
				width: 520rpx;
				height: 180rpx;
				margin: 20rpx 16rpx 0;
			}
		}
	}

	.cate2 {
		margin-bottom: 60rpx;

		.cate2-name {
			height: 110rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #e0e0e0;

			text {
				color: #333;
				margin: 0 30rpx;
			}
		}
	}

	.cate3-wrapper {
		display: flex;
		flex-wrap: wrap;

		.cate3 {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 33.33%;
			margin-bottom: 20rpx;

			image {
				width: 120rpx;
				height: 120rpx;
			}

			text {}
		}
	}
</style>
